package cuiyt.juc.volatiletest;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author cyt
 * @create 2020-05-04 0:14
 */
public class demo03 {
    private volatile static AtomicInteger num = new AtomicInteger();
    // cas 的原子性操作
    public static void increase() {
        num.incrementAndGet();
    }
    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    increase();
                }
            }, String.valueOf(i)).start();
        }
        // 最后线程的存活数量大于 2 的话就证明 上面的 20 条线程还没执行完毕
        while (Thread.activeCount() > 2) {
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName() + num);
    }
}
